package com.fz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fz.entity.Marketing;
import com.fz.service.MarketingService;
import com.fz.vo.ResultVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fz
 * @since 2024-08-26
 */
@RestController
@RequestMapping("/marketing")
public class MarketingController {
    @Resource
    MarketingService marketingService;
    
    @GetMapping("list")
    ResultVo<List<Marketing>> list() {
        return ResultVo.success(marketingService.list());
    }

    @PostMapping("/del")
    ResultVo<Boolean> del(Long id) {
        return ResultVo.success(marketingService.removeById(id), "删除模块成功");
    }

    @PostMapping("/add")
    ResultVo<Boolean> add(String name, String description) {
        return ResultVo.success(marketingService.save(new Marketing(name,description, true)), "添加模块成功");
    }

    @GetMapping("/getById")
    ResultVo<Marketing> getById(Long id) {
        return ResultVo.success(marketingService.getById(id));
    }

    @PostMapping("update")
    ResultVo<Boolean> update(Long id, String name, String description) {
        return ResultVo.success(marketingService.updateById(new Marketing(id,name,description)), "修改模块成功");
    }

    @PostMapping("/addGoods")
    ResultVo<Boolean> addGoods(Long marketingId, Long[] goodsId) {
        return ResultVo.success(marketingService.addGoods(marketingId,goodsId), "修改成功");
    }

    @GetMapping("/searchByName")
    ResultVo<List<Marketing>> searchByName(String searchValue) {
        List<Marketing> marketings = marketingService.searchByName(searchValue);
        if (marketings.isEmpty()) {
            return ResultVo.failed("抱歉，没有为您搜索到");
        }
        return ResultVo.success(marketings, "搜索成功");
    }
}
